package com.heimachefu.module.home.controller;

import com.alibaba.fastjson.JSONObject;
import com.heimachefu.module.user.entity.UserEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @author liuxingdong@heimachefu.com
 * @date 2019/7/19 13:46
 */
@Controller
@RequestMapping("/home")
public class HomeController {


    /**
     * 用户首页
     * @return
     */
    @RequestMapping("/main")
    public ModelAndView main(HttpServletRequest request){
        ModelAndView view = new ModelAndView("app/home/home");
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserEntity userEntity = (UserEntity) authenticationToken.getPrincipal();
        //details里面可能存放了当前登录用户的详细信息，也可以通过cast后拿到
        System.out.println("参数显示为: " + JSONObject.toJSON(authenticationToken.getDetails()));
        Principal principal = request.getUserPrincipal();
        System.out.println("principal = " + principal.getName());
        view.addObject("user",userEntity);
        return view;
    }
}
